unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    btn1: TButton;
    txt1: TStaticText;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  a : array [0..9] of array [0..9] of TButton;
  used : array [0..9] of array [0..9] of Integer;
  GlobalCount : Integer;
  Croses, Zero : Integer;

implementation

{$R *.dfm}



procedure TForm1.FormCreate(Sender: TObject);
var
  I, J : Integer;
begin
  Croses := 0;
  Zero := 0;
  Button1.Visible := False;
  For I := 0 to 9 do
      For J := 0 to 9 do
        begin
          a[I][J] := TButton.Create(Form1);
          a[I][J].parent := Form1;
          a[I][J].Visible := True;
          a[I][J].Left := I * 20;
          a[I][J].Top := J * 20;
          a[I][J].Width := 20;
          a[I][J].Height := 20;
          used[I][J] := 0;
          a[I][J].OnClick := Button1Click;
        end;
  GlobalCount := 0;
  txt1.Caption := 'score is 0 : 0';
end;

function Is_Somebodywon(x, y : Integer) : Integer;
var
  lr, updown, p1p1, p1m1 : Integer;
  I, J : Integer;
begin
  lr := 0;
  updown := 0;
  p1p1 := 0;
  p1m1 := 0;
  I := x;
  J := y;
  while(I < 10) do
      if(used[I][J] = used[x][y])   then
          begin
            lr := lr + 1;
            I := (I + 1);
          end
      else
          break;
  I := x;
  while(I >= 0) do
      if(used[I][J] = used[x][y]) then
          begin
            lr := lr + 1;
            I := I - 1;
          end
      else
          break;
  I := x;
  J := y;
  while(J < 10) do
      if(used[I][J] = used[x][y]) then
          begin
             updown := updown + 1;
             J := J + 1
          end
      else
          break;
  J := y;
  while(J >= 0) do
      if(used[I][J] = used[x][y]) then
          begin
              updown := updown + 1;
              J := J - 1;
          end
      else
          Break;
  if((lr >= 5) or (updown >= 5)) then
      if(used[X][Y] = 1) then
          Is_Somebodywon := 1
      else
          Is_Somebodywon := 2;
  Is_Somebodywon := -1;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
    I, J : Integer;
    I1, J1 : Integer;
    K, L : Integer;
begin
  For I := 0 to 9 do
      For J := 0 to 9 do
          begin
              if(Sender = a[I][J])  then
                  begin
                    txt1.Caption := ' ' + txt1.Caption + IntToStr(I) + ' ' + IntToStr(J) + ' ';
                    if ((GlobalCount mod 2) = 0) then
                      begin
                        a[I][J].Font.Color := clRed;
                        a[I][J].Caption := 'X';
                        used[I][J] := 1;

                      end
                    else
                      begin
                        a[I][J].Font.Color := clBlue;
                        a[I][J].Caption := 'O';
                        used[I][J] := 2;
                      end;
                    GlobalCount := GlobalCount + 1;
                    a[I][J].Enabled := False;
                    I1 := I;
                    J1 := J;
                    if(Is_Somebodywon(I, J) <> -1) then
                      begin
                          if(Is_Somebodywon(I, J) = 1) then
                              Croses := Croses + 1
                          else
                              Zero := Zero + 1;
                          Txt1.Caption := IntToStr(Croses) + ' : ' + IntToStr(Zero) + 'SMBDY WN' + IntToStr(used[0][0]);
                          For K := 0 to 9 do
                              for L := 0 to 9 do
                                  a[I][J].Enabled := True;
                      end;
                  end;


          end;

end;
procedure TForm1.btn1Click(Sender: TObject);
var
  I, J : Integer;
begin

  for I := 0 to 9 do
      for J := 0 to 9 do
        Begin
          a[I][J].Caption := ' ';
          a[I][J].Enabled := True;
        End;
  GlobalCount := 0;

end;


end.
